package one;

import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

import java.io.*;
import java.util.*;

/**
 * @author wangpp
 */
public class ObjectMapperExample {
    public static void main(String[] args) throws IOException {

//        readValue();
//        fromJson();
//        fromFile();

//        writeJsonToString();
//        writeJsonToFile();

//        mapToJson();


//        longToDate();
//        stringToDate();

        dateToJson();

    }

    private static void stringToDate() {
        String paymentJson = "{ \"paymentDate\" : \"2015-06-11T12:33:00.014Z\" }";

        ObjectMapper objectMapper = JsonFactory.create();
        //ObjectMapper objectMapper = JsonFactory.createUseJSONDates();

        Payment payment = objectMapper.readValue(paymentJson, Payment.class);

        System.out.println("payment.paymentDate = " + payment.paymentDate);
    }

    private static void longToDate() {
        ObjectMapper objectMapper = JsonFactory.create();

        String paymentJson = "{ \"paymentDate\" : 1434016456493 }";
        Payment payment = objectMapper.readValue(paymentJson, Payment.class);

        System.out.println(payment);
    }

    private static void dateToJson() {

        Payment p = new Payment();
        p.paymentDate = new Date();

        //1 date to long
        ObjectMapper mapper = JsonFactory.create();
        String j1 = mapper.writeValueAsString(p);
        //{"paymentDate":1599379697223}
        System.out.println(j1);


        //2 date to string format
        ObjectMapper dateMapper = JsonFactory.createUseJSONDates();
        String j2 = dateMapper.writeValueAsString(p);
        //{"paymentDate":"2020-09-06T08:08:17.223Z"}
        System.out.println(j2);


    }

    private static void mapToJson() {
        //1 创建boon的ObjectMapper
        ObjectMapper mapper = JsonFactory.create();

        //2 组装map
        Map car = new HashMap();
        car.put("brand", "BMW");
        car.put("doors", 4);
        List cars = new ArrayList();
        cars.add(car);
        Map fleet = new HashMap();
        fleet.put("cars", cars);

        //3 map to json
        String s = mapper.writeValueAsString(fleet);
        System.out.println(s);

    }

    private static void writeJsonToFile() throws FileNotFoundException {
        //1 创建boon的ObjectMapper
        ObjectMapper mapper = JsonFactory.create();
        //2 将json解析成对象图
        String fleetStr = "{\"cars\":[{\"brand\":\"Audi\",\"doors\":4},{\"brand\":\"Mercedes\",\"doors\":3},{\"brand\":\"BMW\",\"doors\":3}]}";
        Fleet fleet = mapper.fromJson(fleetStr, Fleet.class);

        //3 将对象写入文件
        File outFile = new File("one-2.json");
        mapper.writeValue(outFile, fleet);

        //4 将文件读取回来
        Fleet nFleet = mapper.fromJson(new FileReader(outFile), Fleet.class);
        System.out.println(nFleet);
    }

    private static void writeJsonToString() {
        //1 创建boon的ObjectMapper
        ObjectMapper mapper = JsonFactory.create();
        //2 将json解析成对象图
        String fleetStr = "{\"cars\":[{\"brand\":\"Audi\",\"doors\":4},{\"brand\":\"Mercedes\",\"doors\":3},{\"brand\":\"BMW\",\"doors\":3}]}";
        Fleet fleet = mapper.fromJson(fleetStr, Fleet.class);


        //3 将对象写成json
        String s = mapper.writeValueAsString(fleet);
        System.out.println(s);

    }

    private static void fromFile() throws IOException {
        File file = new File("one.json");
        String fleetStr = "{\"cars\":[{\"brand\":\"Audi\",\"doors\":4},{\"brand\":\"Mercedes\",\"doors\":3},{\"brand\":\"BMW\",\"doors\":3}]}";
        FileWriter writer = new FileWriter(file);
        writer.write(fleetStr);
        writer.flush();
        writer.close();


        ObjectMapper mapper = JsonFactory.create();
        Fleet fleet = mapper.readValue(file, Fleet.class);
        System.out.println(fleet);
    }

    private static void fromJson() {
        //1 创建boon的ObjectMapper
        ObjectMapper mapper = JsonFactory.create();

        //2 将json解析成对象图
        String fleetStr = "{\"cars\":[{\"brand\":\"Audi\",\"doors\":4},{\"brand\":\"Mercedes\",\"doors\":3},{\"brand\":\"BMW\",\"doors\":3}]}";

        Fleet fleet = mapper.fromJson(fleetStr, Fleet.class);
        System.out.println(fleet);

        //3 read to Map
        Map map = mapper.readValue(fleetStr, Map.class);

        //4 转成Car
        List<Car> cars = (List<Car>) map.get("cars");
        System.out.println(cars);

        //5 转成map
        List<Map> carList = (List<Map>) map.get("cars");
        for (Map carMap : carList) {
            String brand = (String) carMap.get("brand");
            int doors = (int) carMap.get("doors");
            System.out.format("%s:%d%n", brand, doors);
        }
    }

    private static void readValue() {
        //1 创建boon的ObjectMapper
        ObjectMapper mapper = JsonFactory.create();

        //2 将json解析成对象图
        String fleetStr = "{\"cars\":[{\"brand\":\"Audi\",\"doors\":4},{\"brand\":\"Mercedes\",\"doors\":3},{\"brand\":\"BMW\",\"doors\":3}]}";
        Fleet fleet = mapper.readValue(fleetStr, Fleet.class);
        System.out.println(fleet);
    }
}
